request = require 'request'
Q = require 'q'
util = require '../Util'

## produce a promise
q_request_post= (config)->
  deferred = Q.defer()

  request.post config,(err,msg,body)->
    if err
      deferred.reject err
    else
      if !body.success
        deferred.reject(body.content.message)
      else
        deferred.resolve(body.content)
  return deferred.promise
option=
  url:'http://121.43.161.94:8180/resources/request/noticeModule-noticeManger@createNotice'
  json:
    content:
      noticeparam:
        content: 'aaaaaaa'
        domain: ''
        expire: 7
        noticeType: 2
        scope: 101
        status: 2
        username:'admin@gcydata.gov.cn'


# chain call
q_request_post(option)
  .then (result)->
    console.log 'the reusult is ',result
    q_request_post(option)



  .then (result)->
    console.log 'second is ',result
  .then ()->
    util.console('00')
  .catch (err)->
    console.error 'failed',err

